package com.dht.xiao.jianye.base.util;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * <br> ClassName:   JsonUtil
 * <br> Description: Json工具
 * <br>
 * <br> Author:      fangbingran
 * <br> Date:        2018/3/25 16:05
 */
public class GsonUtil {

    private Gson mGSon;
    private static GsonUtil sInstance;

    public static synchronized GsonUtil getInstance() {
        if (sInstance == null) {
            sInstance = new GsonUtil();
        }
        return sInstance;
    }

    private GsonUtil() {
        mGSon = new GsonBuilder().disableHtmlEscaping().create();
    }

    public String parseObjToJsonStr(Object srcObj) {
        String result = "";
        if (mGSon == null) {
            mGSon = new GsonBuilder().disableHtmlEscaping().create();
        }
        try {
            result = mGSon.toJson(srcObj);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
        return result;
    }

    public <T> T parseJsonStrToObj(String src, Class<T> classOfT) {
        if (TextUtils.isEmpty(src)) {
            return null;
        }
        T result = null;
        if (mGSon == null) {
            mGSon = new GsonBuilder().disableHtmlEscaping().create();
        }
        try {
            result = mGSon.fromJson(src, classOfT);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
        return result;
    }

    public <T> T parseJsonElementToObj(JsonElement src, Type superClass) {
        T result = null;
        if (mGSon == null) {
            mGSon = new GsonBuilder().disableHtmlEscaping().create();
        }
        try {
            result = mGSon.fromJson(src, superClass);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
        return result;
    }

    public <T> T parseJsonStrToObj(String json, Type typeOfT) {
        T result = null;
        if (mGSon == null) {
            mGSon = new GsonBuilder().disableHtmlEscaping().create();
        }
        try {
            result = mGSon.fromJson(json, typeOfT);
        } catch (Exception e) {
            Log.getStackTraceString(e);
        }
        return result;
    }

    public <T> List<T> parseJsonToList(JsonElement jsonData, Class<T> cls) {
        List<T> list = new ArrayList();
        Gson gson = new Gson();
        // 将JsonElement转换成JsonArray
        JsonArray jsonArray = jsonData.getAsJsonArray();
        // Iterator处理
        Iterator it = jsonArray.iterator();
        while (it.hasNext()) {
            // 提取JsonElement
            JsonElement jsonElement = (JsonElement) it.next();
            // JsonElement转换成String
            String json = jsonElement.toString();
            // String转化成JavaBean
            T bean = gson.fromJson(json, cls);
            list.add(bean);
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public <T> List<T> fromJsonArray(String json, Class<T> clazz) throws Exception {
        List<T> lst = new ArrayList<>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for (final JsonElement elem : array) {
            lst.add(mGSon.fromJson(elem, clazz));
        }
        return lst;
    }

    public <T> List<T> fromJsonArray(String json, Type typeOfT) throws Exception {
        return mGSon.fromJson(json, typeOfT);
    }
}
